package zzs.generalframework.console.core.parameter.commonparameter.censusregistertype.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import zzs.generalframework.common.core.parameter.commonparameter.censusregistertype.CensusRegisterTypeInfo;
import zzs.generalframework.common.core.parameter.commonparameter.censusregistertype.CensusRegisterTypePO;
import zzs.generalframework.console.core.parameter.commonparameter.censusregistertype.dao.ICensusRegisterTypeDao;
import zzs.generalframework.console.util.QueryParameter;
import zzs.generalframework.util.core.string.StringUtils;

/**
 * @title
 * @description
 * @project zzs-console
 * @package zzs.generalframework.console.core.parameter.commonparameter.censusregistertype.dao.impl
 * @author zzs
 * @date 2018年2月2日
 * @version 1.0
 */
@Repository("zzs.generalframework.console.core.parameter.commonparameter.censusregistertype.dao.impl.censusRegisterTypeDaoImpl")
public class CensusRegisterTypeDaoImpl implements ICensusRegisterTypeDao
{
	private Logger logger = LoggerFactory.getLogger(CensusRegisterTypeDaoImpl.class);
	@PersistenceContext(unitName = "zzs-entity")
	EntityManager entityManager;
	@Autowired
	JdbcTemplate jdbcTemplate;

	@Override
	public boolean insert(CensusRegisterTypePO censusRegisterTypePO)
	{
		logger.info("====插入户口类别数据开始====");
		logger.debug("====插入户口类别数据,入参:CensusRegisterTypePO=[{}]====", censusRegisterTypePO);
		entityManager.persist(censusRegisterTypePO);
		logger.debug("====插入户口类别数据,出参:boolean=[{}]====", true);
		logger.info("====插入户口类别数据结束====");
		return true;
	}

	@Override
	public boolean update(CensusRegisterTypePO censusRegisterTypePO)
	{
		logger.info("====更新户口类别数据开始====");
		logger.debug("====更新户口类别数据,入参:CensusRegisterTypePO=[{}]====", censusRegisterTypePO);
		entityManager.merge(censusRegisterTypePO);
		logger.debug("====更新数据,出参:boolean=[{}]====", true);
		logger.info("====更新户口类别数据结束====");
		return true;
	}

	@Override
	public List<CensusRegisterTypePO> select(CensusRegisterTypeInfo censusRegisterTypeInfo)
	{
		logger.info("====查询户口类别数据开始====");
		logger.debug("====查询户口类别数据,入参:CensusRegisterTypeInfo=[{}]====", censusRegisterTypeInfo);
		List<CensusRegisterTypePO> returnList = null;
		StringBuffer sqlBuffer = new StringBuffer();
		QueryParameter parameter = new QueryParameter();
		sqlBuffer.append("select a from CensusRegisterTypePO a where 1=1 ");
		if (StringUtils.isNotNullAndBlank(censusRegisterTypeInfo.getCensusRegisterTypeID()))
		{
			sqlBuffer.append(" and a.censusRegisterTypeID=?");
			parameter.putParameterObject(censusRegisterTypeInfo.getCensusRegisterTypeID());
		}
		if (StringUtils.isNotNullAndBlank(censusRegisterTypeInfo.getCensusRegisterCode()))
		{
			sqlBuffer.append(" and a.censusRegisterCode=?");
			parameter.putParameterObject(censusRegisterTypeInfo.getCensusRegisterCode());
		}
		if (StringUtils.isNotNullAndBlank(censusRegisterTypeInfo.getCensusRegisterName()))
		{
			sqlBuffer.append(" and a.censusRegisterName=?");
			parameter.putParameterObject(censusRegisterTypeInfo.getCensusRegisterName());
		}
		if (StringUtils.isNotNullAndBlank(censusRegisterTypeInfo.getOrderNumber()))
		{
			sqlBuffer.append(" and a.orderNumber=?");
			parameter.putParameterObject(censusRegisterTypeInfo.getOrderNumber());
		}
		if (StringUtils.isNotNullAndBlank(censusRegisterTypeInfo.getRecordStatus()))
		{
			sqlBuffer.append(" and a.recordStatus=?");
			parameter.putParameterObject(censusRegisterTypeInfo.getRecordStatus());
		}
		logger.info("查询数据SQL:[{}]", sqlBuffer.toString());
		Query query = entityManager.createQuery(sqlBuffer.toString());
		parameter.setQueryParameter(query);
		returnList = query.getResultList();
		logger.debug("====查询户口类别数据,出参:List<CensusRegisterTypePO>=[{}]====", returnList);
		logger.info("====查询户口类别数据结束====");
		return returnList;
	}

	@Override
	public boolean delete(CensusRegisterTypePO censusRegisterTypePO)
	{
		logger.info("====删除户口类别数据开始====");
		logger.debug("====删除户口类别数据,入参:CensusRegisterTypePO=[{}]====", censusRegisterTypePO);
		entityManager.remove(censusRegisterTypePO);
		logger.debug("====删除户口类别数据,出参:boolean=[{}]====", true);
		logger.info("====删除户口类别数据结束====");
		return true;
	}

	@Override
	public Long count(CensusRegisterTypePO censusRegisterTypePO)
	{
		logger.info("====统计户口类别数据开始====");
		logger.debug("====统计户口类别数据,入参:CensusRegisterTypePO=[{}]====", censusRegisterTypePO);
		Long returnLong = null;
		StringBuffer sqlBuffer = new StringBuffer();
		QueryParameter parameter = new QueryParameter();
		sqlBuffer.append("select count(*) from CensusRegisterTypePO a where 1=1 ");
		if (StringUtils.isNotNullAndBlank(censusRegisterTypePO.getCensusRegisterTypeID()))
		{
			sqlBuffer.append(" and a.censusRegisterTypeID=?");
			parameter.putParameterObject(censusRegisterTypePO.getCensusRegisterTypeID());
		}
		if (StringUtils.isNotNullAndBlank(censusRegisterTypePO.getCensusRegisterCode()))
		{
			sqlBuffer.append(" and a.censusRegisterCode=?");
			parameter.putParameterObject(censusRegisterTypePO.getCensusRegisterCode());
		}
		if (StringUtils.isNotNullAndBlank(censusRegisterTypePO.getCensusRegisterName()))
		{
			sqlBuffer.append(" and a.censusRegisterName=?");
			parameter.putParameterObject(censusRegisterTypePO.getCensusRegisterName());
		}
		if (StringUtils.isNotNullAndBlank(censusRegisterTypePO.getOrderNumber()))
		{
			sqlBuffer.append(" and a.orderNumber=?");
			parameter.putParameterObject(censusRegisterTypePO.getOrderNumber());
		}
		if (StringUtils.isNotNullAndBlank(censusRegisterTypePO.getRecordStatus()))
		{
			sqlBuffer.append(" and a.recordStatus=?");
			parameter.putParameterObject(censusRegisterTypePO.getRecordStatus());
		}
		logger.info("统计数据SQL:[{}]", sqlBuffer.toString());
		Query query = entityManager.createQuery(sqlBuffer.toString());
		parameter.setQueryParameter(query);
		returnLong = (Long) query.getSingleResult();
		logger.debug("====统计户口类别数据,出参:Long=[{}]====", returnLong);
		logger.info("====统计户口类别数据结束====");
		return returnLong;
	}

}
